﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace winautorun
{
    public partial class Form1 : Form
    {
        string fileName = "开机启动.exe";

        public Form1()
        {   
            InitializeComponent();
            this.Load += Form1_Load;
        }

        void Form1_Load(object sender, EventArgs e)
        {
            RefreshButton();
        }
        void RefreshButton()
        {
            btnAdd.Enabled = true;
            btnDelete.Enabled = true;

            bool isSet = CheckExistRegisterApp(fileName);
            if (isSet)
                btnAdd.Enabled = false;
            else
                btnDelete.Enabled = false;
        }
        public static void SetRegistryApp(string ShortFileName)
        {
            try
            {
                Microsoft.Win32.RegistryKey Reg;

                Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (Reg == null)
                {   
                    Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
                }
                Reg.SetValue(ShortFileName, Application.ExecutablePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static bool DeleteRegisterApp(string ShortFileName)
        {
            try
            {
                Microsoft.Win32.RegistryKey Reg;
                Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (Reg == null)
                {
                    Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
                }
                Reg.DeleteValue(ShortFileName, false);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        /// <summary>  
        ///     检查当前程序是否在启动项中  
        /// </summary>  
        /// <returns></returns>  
        public static bool CheckExistRegisterApp(string ShortFileName)
        {
            bool bResult = false;

            try
            {
                Microsoft.Win32.RegistryKey Reg;
                Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                if (Reg == null)
                {
                    Reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
                }

                foreach (string s in Reg.GetValueNames())
                {
                    if (s.Equals(ShortFileName))
                    {
                        bResult = true;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return bResult;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SetRegistryApp(fileName);
            RefreshButton();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DeleteRegisterApp(fileName);
            RefreshButton();
        }
    }
}
